import {CommonFunction} from "./CommonFunction";
import {GlobalClass} from "./GlobalClass";
import {EventDefinition} from "./EventDefinition";
import {GlobalDefinition} from "./GlobalDefinition";
import {MainCanvasClass } from "./MianCanvasClass";
import {OG} from "./CommonClass";
import { DataRecordClass } from "./DataRecordClass";

/**属性框控制类 */
export class AttributeClass {
    private CF:CommonFunction;

    private BlockWidth: HTMLInputElement;
    private BlockHeight: HTMLInputElement;
    private BlockWidthSum: HTMLInputElement;
    private BlockHeightSum: HTMLInputElement;
    private BlockStartIndex: HTMLInputElement;
    private TagBgColor: HTMLDivElement;
    private Container: HTMLDivElement;
    private CloseColoPicker: HTMLButtonElement;
    private SetCanvasW: HTMLInputElement;
    private SetCanvasH: HTMLInputElement;
    private SaveArrayBtn: HTMLInputElement;
    private SaveJsonBtn: HTMLInputElement;

    public constructor () {
        this.CF = CommonFunction.ins();
        this.InitAttribute();
        this.InitListenet();
        this.InitDefault();
    }

    /**根据id获取属性框各部分元素*/ 
    private InitAttribute () {
        this.BlockWidth = this.CF.IdToElement("setBlockWidth") as HTMLInputElement;
        this.BlockHeight = this.CF.IdToElement("setBlockHeight") as HTMLInputElement;
        this.BlockWidthSum = this.CF.IdToElement("sumWidth") as HTMLInputElement;
        this.BlockHeightSum = this.CF.IdToElement("sumHeight") as HTMLInputElement;
        this.BlockStartIndex = this.CF.IdToElement("setBlockStartIndex") as HTMLInputElement;
        this.SetCanvasW = this.CF.IdToElement("setCanvasW") as HTMLInputElement;
        this.SetCanvasH = this.CF.IdToElement("setCanvasH") as HTMLInputElement;
        this.TagBgColor = this.CF.IdToElement("tagBgColor") as HTMLDivElement;
        this.Container = this.CF.IdToElement("container") as HTMLDivElement;
        this.CloseColoPicker = this.CF.IdToElement("closeColoPicker") as HTMLButtonElement;
        this.SaveArrayBtn = this.CF.IdToElement("saveArrayBtn") as HTMLInputElement;
        this.SaveJsonBtn = this.CF.IdToElement("saveJsonBtn") as HTMLInputElement;

    }

    /**初始化默认值 */
    private InitDefault () {
        this.BlockWidth.value = GlobalDefinition.BlockWidth + "";
        this.BlockHeight.value = GlobalDefinition.BlockHeight + "";
        this.BlockWidthSum.value = GlobalDefinition.BlockWidthSum + "";
        this.BlockHeightSum.value = GlobalDefinition.BlockHeightSum + "";
        this.BlockStartIndex.value = GlobalDefinition.BlockStartIndex + "";
        this.SetCanvasH.value = GlobalDefinition.MainCanvasHeight + "";
        this.SetCanvasW.value = GlobalDefinition.MainCanvasWidth + "";
    }

    private InitListenet () {
        //点击类型事件元素
        let clickTypeElements:any[] = [this.TagBgColor,this.Container,this.CloseColoPicker,this.SaveArrayBtn,this.SaveJsonBtn];
        //失去焦点类型事件元素
        let blurTypesElements:any[] = [this.BlockHeight,this.BlockWidth,this.SetCanvasW,this.SetCanvasH];
        this.CF.ListenerTypeEvents(clickTypeElements,EventDefinition.CLICK,this.onCallBack.bind(this));
        this.CF.ListenerTypeEvents(blurTypesElements,EventDefinition.BLUR,this.onCallBack.bind(this));
    }

    /**点击回调事件 */
    private onCallBack (e) {
        let event = e[0];
        switch(event) {
            case this.TagBgColor:
                let tag:string = this.Container.style.display;
                if(tag == "none") {
                    this.Container.style.display = "";
                }
            break;
            case this.BlockHeight:
                let height: number = parseInt(this.BlockHeight.value);
                GlobalDefinition.BlockHeight = height;
                this.AdjustmentMCanvas();
                MainCanvasClass.ins().CenterMainCanvas();
                MainCanvasClass.ins().UpdateDrawGrid();
            break;
            case this.BlockWidth:
                let width: number = parseInt(this.BlockWidth.value);
                GlobalDefinition.BlockWidth = width;
                this.AdjustmentMCanvas();
                MainCanvasClass.ins().CenterMainCanvas();
                MainCanvasClass.ins().UpdateDrawGrid();
            break;
            case this.CloseColoPicker:
                //关闭拾色器
                this.Container.style.display = "none";
            break;
            case this.SetCanvasW:
                let m_w:number = parseInt(this.SetCanvasW.value);
                MainCanvasClass.ins().SetWidth(m_w);
                MainCanvasClass.ins().CenterMainCanvas();
                this.AdjustmentBlockWH();
                MainCanvasClass.ins().UpdateDrawGrid();
            break;
            case this.SetCanvasH:
                let m_h:number = parseInt(this.SetCanvasH.value);
                MainCanvasClass.ins().SetHeight(m_h);
                MainCanvasClass.ins().CenterMainCanvas();
                this.AdjustmentBlockWH();
                MainCanvasClass.ins().UpdateDrawGrid();
            break;
            case this.SaveArrayBtn:
                //同步地图数组数据
                DataRecordClass.ins().MapArrUpdate();
                this.OpenEject();
                //alert("保存数组!");
            break;
            case this.SaveJsonBtn:
                DataRecordClass.ins().MapJsonUpdate();
                this.OpenEject();
                //alert("保存Json!");
            break;
        }
    }

    /**根据当前mainCanvas返回的宽高调整图块显示数据 */
    private AdjustmentBlockWH () {
        let hv:OG.Size = MainCanvasClass.ins().ReturnCanvasHV();
        let block_w:number = parseInt(this.BlockWidth.value);
        let block_h:number = parseInt(this.BlockHeight.value);
        let sum_w:number = parseInt((hv.width / block_w) + "");
        let sum_h:number = parseInt((hv.height / block_h) + "");
        this.BlockWidthSum.value = sum_w + "";
        this.BlockHeightSum.value = sum_h + "";
        GlobalDefinition.BlockHeightSum = sum_h;
        GlobalDefinition.BlockWidthSum = sum_w;
        DataRecordClass.ins().MapDataReset();  //有图块与画布宽高的调整则重置地图数据
    }

    /**根据当前图块宽高显示数据调整mainCanvas的宽高 */
    private AdjustmentMCanvas () {
        let block_w:number = parseInt(this.BlockWidth.value);
        let block_h:number = parseInt(this.BlockHeight.value);
        let sum_w:number = parseInt(this.BlockWidthSum.value + "");
        let sum_h:number = parseInt(this.BlockHeightSum.value + "");
        MainCanvasClass.ins().SetWidth(block_w * sum_w);
        MainCanvasClass.ins().SetHeight(block_h * sum_h);
        this.SetCanvasW.value = (block_w * sum_w) + "";
        this.SetCanvasH.value = (block_h * sum_h) + "";
    }

    /**打开导出弹框 */
    private OpenEject () {
        let eject:HTMLDivElement = this.CF.IdToElement("eject-area") as HTMLDivElement;
        let tag:string = eject.style.display;
        if(tag == "none") {
            eject.style.display = "";
        }else if(tag == ""){
            eject.style.display = "none";
        }
    }
    
}